package com.clx.common.utils;


import io.minio.*;
import io.minio.errors.MinioException;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import io.minio.messages.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


@Component
@Slf4j
public class MinioUtils {

    @Autowired
    private MinioClient minioClient;
    /**
     * 删除文件
     *
     * @param bucketName
     * @param objectName 文件名称
     * @throws Exception
     */
    public void deleteFile(String bucketName, String objectName) throws Exception {
        try {
            minioClient.removeObject(
                    RemoveObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .build()
            );
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
        }
    }

    /**
     * 删除文件夹
     * @param bucketName
     * @param folderName  文件夹名称
     * @throws Exception
     */
    public void deleteFolder(String bucketName, String folderName) throws Exception {
        try {
            Iterable<Result<Item>> results =
                    minioClient.listObjects(
                            ListObjectsArgs.builder()
                                    .bucket(bucketName)
                                    .prefix(folderName)
                                    .recursive(true)
                                    .build()
                    );

            for (Result<Item> result : results) {
                Item item = result.get();
                minioClient.removeObject(
                        RemoveObjectArgs.builder()
                                .bucket(bucketName)
                                .object(item.objectName())
                                .build()
                );
                log.info("Deleted object: {}", item.objectName());
            }
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
        }
    }


    /**
     * 判断桶是否存在
     *
     * @param bucketName bucket名称
     * @return true存在，false不存在
     */
    public Boolean bucketExists(String bucketName) {
        try {
            return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        } catch (Exception e) {
            throw new RuntimeException("检查桶是否存在失败!", e);
        }
    }


    /**
     * 创建bucket
     *
     * @param bucketName bucket名称
     */
    public void createBucket(String bucketName) {
        if (!this.bucketExists(bucketName)) {
            try {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            } catch (Exception e) {
                throw new RuntimeException("创建桶失败!", e);
            }
        }
    }


    /**
     * 上传文件
     *
     * @param fileName  文件名
     * @param stream      文件流
     * @param contentType 文件类型, 例如 image/jpeg: jpg图片格式, 详细可看: https://www.runoob.com/http/http-content-type.html
     * @return 文件url
     */
    public void putObject(String fileName, InputStream stream, String contentType, String bucketName, MultipartFile multipartFile) {
        try {
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .contentType(contentType)
                            .stream(stream, multipartFile.getSize(), -1)
                            .build()
            );
        } catch (Exception e) {
            throw new RuntimeException("上传文件错误", e);
        }
//        return minioUrl + "/" + bucketName + "/" + fileName;
    }


    /**
     * 下载文件
     * @param bucketName 桶名称
     * @param objectName 文件名
     */
    public InputStream downLoadFile(String bucketName, String objectName) throws Exception {
        GetObjectArgs getObjectArgs = GetObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build();
        return minioClient.getObject(getObjectArgs);
    }
}
